<?php

/**
 * Description of PublicAction
 *
 * @author Administrator
 */
class PublicAction extends Action {

    function _initialize() {
        header('Content-type:text/html; charset=utf8');
        Load('extend'); //加载扩展函数库
        import('@.ORG.RBAC');
        if (!RBAC::AccessDecision()) {
            $this->assign('jumpUrl', U('Public/login'));
            $this->error('你没有权限！');
        }
    }

    public function index() {
        //如果通过认证跳转到首页
        redirect(__APP__);
    }

    //put your code here
    public function verify() {
        import('@.ORG.Image');
        import('@.ORG.String');
        Image::buildImageVerify();
    }

    /**
      +----------------------------------------------------------
     * 管理登陆界面
      +----------------------------------------------------------
     */
    public function login() {
       //dump($_SESSION);
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->display();
        } else {
            $this->redirect(U('Index/index'));
        }
    }

    /**
     * 登陆处理
     */
    public function doLogin() {
        //dump($_SESSION);
        if (empty($_POST['account'])) {
            $this->error('请输入帐号!');
        } elseif (empty($_POST['password'])) {
            $this->error('请输入密码！');
        } elseif (empty($_POST['verify'])) {
            $this->error('请填写验证码！');
        }

        //生成认证条件
        $map = array();
        $map['account'] = $_POST['account'];
        $map['status'] = array('eq', 1);
        if ($_SESSION['verify'] != md5($_POST['verify'])) {
            $this->error('验证码错误！');
        }
        import('@.ORG.RBAC');
        $authInfo = RBAC::authenticate($map);

        if (false === $authInfo) {
            $this->error('帐号不存在或禁用');
        } else {
            if ($authInfo['password'] != md5($_POST['password'])) {
                $this->error('密码错误！');
            }
            $_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];
            $_SESSION['email'] = $authInfo['email'];
            $_SESSION['loginUserName'] = $authInfo['nickname'];
            $_SESSION['lastLoginTime'] = $authInfo['last_login_time'];
            $_SESSION['login_count'] = $authInfo['login_count'];
            if ($authInfo['account'] == 'admin') {
                $_SESSION['administrator'] = true;
            }
            //保存登陆信息
            $user = M('User');
            $ip = get_client_ip();
            $time = time();
            $data = array();
            $data['id'] = $authInfo['id'];
            $data['last_login_time'] = $time;
            $data['last_login_ip'] = $ip;
            $data['login_count'] = array('exp', 'login_count+1');

            $user->save($data);
            //缓存权限访问
            RBAC::saveAccessList();
            $this->success('登陆成功！');
        }
    }

    /**
     * 退出登陆
     */
    public function logout() {
        if (isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
            session_destroy();
            $this->assign('jumpUrl', __URL__ . '/login/');
            $this->success('退出成功！');
        } else {
            $this->error('退出失败！');
        }
    }

}

?>
